/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controladores;

import controladores.exceptions.NonexistentEntityException;
import entidades.IngresoMaterial;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entidades.LoteIngresoMaterial;
import entidades.Material;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author g
 */
public class IngresoMaterialJpaController implements Serializable {

    public IngresoMaterialJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(IngresoMaterial ingresoMaterial) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            LoteIngresoMaterial loteIngresoMaterial = ingresoMaterial.getLoteIngresoMaterial();
            if (loteIngresoMaterial != null) {
                loteIngresoMaterial = em.getReference(loteIngresoMaterial.getClass(), loteIngresoMaterial.getId());
                ingresoMaterial.setLoteIngresoMaterial(loteIngresoMaterial);
            }
            Material material = ingresoMaterial.getMaterial();
            if (material != null) {
                material = em.getReference(material.getClass(), material.getId());
                ingresoMaterial.setMaterial(material);
            }
            em.persist(ingresoMaterial);
            if (loteIngresoMaterial != null) {
                loteIngresoMaterial.getLs_ingresos().add(ingresoMaterial);
                loteIngresoMaterial = em.merge(loteIngresoMaterial);
            }
            if (material != null) {
                material.getLs_ingresos().add(ingresoMaterial);
                material = em.merge(material);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(IngresoMaterial ingresoMaterial) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            IngresoMaterial persistentIngresoMaterial = em.find(IngresoMaterial.class, ingresoMaterial.getId());
            LoteIngresoMaterial loteIngresoMaterialOld = persistentIngresoMaterial.getLoteIngresoMaterial();
            LoteIngresoMaterial loteIngresoMaterialNew = ingresoMaterial.getLoteIngresoMaterial();
            Material materialOld = persistentIngresoMaterial.getMaterial();
            Material materialNew = ingresoMaterial.getMaterial();
            if (loteIngresoMaterialNew != null) {
                loteIngresoMaterialNew = em.getReference(loteIngresoMaterialNew.getClass(), loteIngresoMaterialNew.getId());
                ingresoMaterial.setLoteIngresoMaterial(loteIngresoMaterialNew);
            }
            if (materialNew != null) {
                materialNew = em.getReference(materialNew.getClass(), materialNew.getId());
                ingresoMaterial.setMaterial(materialNew);
            }
            ingresoMaterial = em.merge(ingresoMaterial);
            if (loteIngresoMaterialOld != null && !loteIngresoMaterialOld.equals(loteIngresoMaterialNew)) {
                loteIngresoMaterialOld.getLs_ingresos().remove(ingresoMaterial);
                loteIngresoMaterialOld = em.merge(loteIngresoMaterialOld);
            }
            if (loteIngresoMaterialNew != null && !loteIngresoMaterialNew.equals(loteIngresoMaterialOld)) {
                loteIngresoMaterialNew.getLs_ingresos().add(ingresoMaterial);
                loteIngresoMaterialNew = em.merge(loteIngresoMaterialNew);
            }
            if (materialOld != null && !materialOld.equals(materialNew)) {
                materialOld.getLs_ingresos().remove(ingresoMaterial);
                materialOld = em.merge(materialOld);
            }
            if (materialNew != null && !materialNew.equals(materialOld)) {
                materialNew.getLs_ingresos().add(ingresoMaterial);
                materialNew = em.merge(materialNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = ingresoMaterial.getId();
                if (findIngresoMaterial(id) == null) {
                    throw new NonexistentEntityException("The ingresoMaterial with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            IngresoMaterial ingresoMaterial;
            try {
                ingresoMaterial = em.getReference(IngresoMaterial.class, id);
                ingresoMaterial.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The ingresoMaterial with id " + id + " no longer exists.", enfe);
            }
            LoteIngresoMaterial loteIngresoMaterial = ingresoMaterial.getLoteIngresoMaterial();
            if (loteIngresoMaterial != null) {
                loteIngresoMaterial.getLs_ingresos().remove(ingresoMaterial);
                loteIngresoMaterial = em.merge(loteIngresoMaterial);
            }
            Material material = ingresoMaterial.getMaterial();
            if (material != null) {
                material.getLs_ingresos().remove(ingresoMaterial);
                material = em.merge(material);
            }
            em.remove(ingresoMaterial);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<IngresoMaterial> findIngresoMaterialEntities() {
        return findIngresoMaterialEntities(true, -1, -1);
    }

    public List<IngresoMaterial> findIngresoMaterialEntities(int maxResults, int firstResult) {
        return findIngresoMaterialEntities(false, maxResults, firstResult);
    }

    private List<IngresoMaterial> findIngresoMaterialEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(IngresoMaterial.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public IngresoMaterial findIngresoMaterial(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(IngresoMaterial.class, id);
        } finally {
            em.close();
        }
    }

    public int getIngresoMaterialCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<IngresoMaterial> rt = cq.from(IngresoMaterial.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
